﻿using YooAsset;

namespace LFrameworkAOT 
{
    internal class FsmClearCacheBundle : IStateNode
    {
        private StateMachine m_Machine;
        PatchOperation m_PatchOperation;

        void IStateNode.OnCreate(StateMachine machine)
        {
            m_Machine = machine;
            m_PatchOperation = m_Machine.Owner as PatchOperation;
        }

        void IStateNode.OnEnter()
        {
            m_PatchOperation.ClearDownloadCache();
            var packageName = (string)m_Machine.GetBlackboardValue("PackageName");
            var package = YooAssets.GetPackage(packageName);
            var operation = package.ClearCacheFilesAsync(EFileClearMode.ClearUnusedBundleFiles);
            operation.Completed += Operation_Completed;
        }
        void IStateNode.OnUpdate()
        {
        }
        void IStateNode.OnExit()
        {
        }

        private void Operation_Completed(YooAsset.AsyncOperationBase obj)
        {
            m_Machine.ChangeState<FsmStartGame>();
        }
    }
}
